package edu.ccut.saturn.studio.logic.model.saturn.logic;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

import edu.ccut.saturn.studio.Activator;
import edu.ccut.saturn.studio.Images;
import edu.ccut.saturn.studio.core.util.xml.SaturnW3CUtil;
import edu.ccut.saturn.studio.dialog.startdialog.SaturnStartDialog;
import edu.ccut.saturn.studio.logic.LogicMultiPageEditor;
import edu.ccut.saturn.studio.logic.figure.PageFigure;
import edu.ccut.saturn.studio.logic.model.Shape;
import edu.ccut.saturn.studio.logic.model.ShapesDiagram;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnParameter;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnStartNode;
import edu.ccut.saturn.studio.logic.model.saturn.action.SaturnStartNodeInAction;

public class SaturnStartNodeInLogic extends SaturnStartNode {

	@Override
	public Shape cloneShape(Point mousePoint, Shape shape) {
		ShapesDiagram diagram = (ShapesDiagram) shape.getParent();
		if (diagram.getEditors() instanceof LogicMultiPageEditor) {
			SaturnStartNodeInLogic saturnStartNode = new SaturnStartNodeInLogic();
			SaturnStartNodeInLogic saturn = (SaturnStartNodeInLogic) shape;
			saturnStartNode.setLocation(mousePoint);
			saturnStartNode.setName(saturn.getName());
			saturnStartNode.setAutorollback(saturn.getAutorollback());
			saturnStartNode.setDescription(saturn.getDescription());
			saturnStartNode.setParameters(saturn.getParameters());
			saturnStartNode.setId(saturn.getId());
			saturnStartNode.setNextId(saturn.getNextId());
			return saturnStartNode;
		} else {
			SaturnStartNodeInAction saturnStartNode = new SaturnStartNodeInAction();
			SaturnStartNodeInLogic saturn = (SaturnStartNodeInLogic) shape;
			saturnStartNode.setLocation(mousePoint);
			saturnStartNode.setName(saturn.getName());
			saturnStartNode.setAutorollback(saturn.getAutorollback());
			saturnStartNode.setDescription(saturn.getDescription());
			saturnStartNode.setParameters(saturn.getParameters());
			saturnStartNode.setId(saturn.getId());
			saturnStartNode.setNextId(saturn.getNextId());
			return saturnStartNode;
		}
	}

	private static final long serialVersionUID = 1L;

	@SuppressWarnings("unchecked")
	@Override
	public Dialog getDialog() {
		SaturnStartDialog dialog = new SaturnStartDialog(super.getShell(), this);
		super.reorder(this);
		return dialog;
	}

	private Image image = Activator.getImage(Images.START_ICON);

	public IFigure getFigure() {
		PageFigure figure = new PageFigure();
		if (super.isIschanged() == false) {
			Label imageLabel = new Label(image);
			imageLabel.setLocation(new Point(figure.getLocation().x + 90,
					figure.getLocation().y));
			figure.getImageFigure().add(imageLabel);
			Font nameFont = new Font(null, "Arial", 74, SWT.BOLD);
			Label namelabel = new Label(super.getName());
			namelabel.setFont(nameFont);
			figure.getLetterFigure().add(namelabel);
		}

		return figure;
	}

	public List<SaturnParameter> getListParameter(Document document) {

		List<SaturnParameter> paramaters = new ArrayList<SaturnParameter>();

		Node root = document.getDocumentElement();
		Node StartNode = SaturnW3CUtil.getFirstChildByName(root, "start");
		Node ParamatersNode = SaturnW3CUtil.getFirstChildByName(StartNode,
				"parameters");
		List<Node> Paramaters = SaturnW3CUtil.getChildrenByName(ParamatersNode,
				"parameter");
		for (Node parameterNode : Paramaters) {
			SaturnParameter parameter = new SaturnParameter();
			parameter.setId(SaturnW3CUtil.getNodeAttributeValue(parameterNode,
					"id"));
			parameter.setName(SaturnW3CUtil.getNodeAttributeValue(parameterNode,
					"name"));
			parameter.setClassType(SaturnW3CUtil.getNodeAttributeValue(
					parameterNode, "classType"));
			parameter.setPassingType(SaturnW3CUtil.getNodeAttributeValue(
					parameterNode, "passingType"));
			parameter.setValueKey(SaturnW3CUtil.getChildNodeValue(parameterNode,
					"value-key"));
			parameter.setScope(SaturnW3CUtil.getChildNodeValue(parameterNode,
					"scope"));
			parameter.setDescription(SaturnW3CUtil.getChildNodeValue(
					parameterNode, "description"));
			paramaters.add(parameter);

		}
		return paramaters;
	}
}
